package icu.jiapeng.DemoJava.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


@Configuration
@Slf4j
public class TaskConfig {
    @Bean
    public Executor taskExecutor() {
        //根据可用线程数配置一个线程池,优化参数
        //设置空闲线程数，异常异常处理，队列满的情况
        int threadNum = Runtime.getRuntime().availableProcessors();
        int core = threadNum<=4?(int) 1.5*threadNum : threadNum;
        return new ThreadPoolExecutor(core, core * 2, 60, TimeUnit.MICROSECONDS, new ArrayBlockingQueue<>(200), new ThreadPoolExecutor.CallerRunsPolicy(){
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                log.info("线程池已满，开始进行调用者自执行");
            }
        });
    }
}
